package com.example.watchdog;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.os.Handler;
import android.util.Log;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import butterknife.BindView;
import butterknife.ButterKnife;

public class MainActivity extends AppCompatActivity {
    // Used to load the 'native-lib' library on application startup.
    static {
        System.loadLibrary("native-lib");
    }

    @BindView(R.id.watchdog_open)
    Button watchdog_open;
    @BindView(R.id.watchdog_feed)
    Button watchdog_feed;
    @BindView(R.id.watchdog_feeding)
    Button watchdog_feeding;
    @BindView(R.id.watchdog_stop)
    Button watchdog_stop;
    @BindView(R.id.set_time)
    Button set_time;
    @BindView(R.id.stop_feeding)
    Button stop_feeding;
    @BindView(R.id.watchdog_status)
    TextView watchdog_status;
    @BindView(R.id.watchdog_count)
    TextView watchdog_count;
    @BindView(R.id.time_out)
    EditText time_out;
    @BindView(R.id.feed_time)
    EditText feed_time;
    private CountDownTimer countDownTimer;
    private boolean isPing = false;
    private boolean isOpen = false;
    private Handler handler = new Handler();
    private Runnable runnable;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
        watchdog_open.setOnClickListener(v -> openWatchDog());
        watchdog_feed.setOnClickListener(v -> feedWatchDog() );
        set_time.setOnClickListener(v ->setWatchdogTime() );
        watchdog_feeding.setOnClickListener(v -> feedingWatchDog());
        stop_feeding.setOnClickListener(v -> {handler.removeCallbacks(runnable);});
        watchdog_stop.setOnClickListener(v -> {
            stopWatchdog();
            isCountDownTimer();
            isOpen = false;});
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        stopWatchdog();
    }

    private void openWatchDog(){
        startWatchdog();
        watchdog_status.setText("open watchdog success");
        set_Timer();
        isOpen = true;
    }

    private void feedWatchDog(){
        pingWatchdog();
        watchdog_status.setText("feed watchdog success");
        set_Timer();
    }
    private void feedingWatchDog(){
        if (isOpen){
            final int second = Integer.parseInt(feed_time.getText().toString()) * 1000;
            if (!isPing){
                isPing = true;
                runnable = new Runnable() {
                    @Override
                    public void run() {
                        feedWatchDog();
                        handler.postDelayed(this,second);
                    }
                };
                handler.postDelayed(runnable,second);
            }
        }else{
            Toast.makeText(this, "please open watchdog", Toast.LENGTH_SHORT).show();
        }
    }
    private void feedingStop(){

    }
    private void setWatchdogTime(){
        if (!time_out.getText().toString().isEmpty()){
            setWatchDogTimeout(time_out.getText().toString());
            watchdog_status.setText("set timeout success");
            set_Timer();
        }else {
            Toast.makeText(this,"time is null",Toast.LENGTH_SHORT).show();
        }
    }

    private void set_Timer(){
        isCountDownTimer();
        countDownTimer = new CountDownTimer(Long.parseLong(getWatchDogTimeOut()+"000"), 1000) {
            @Override
            public void onTick(long millisUntilFinished) {
                watchdog_count.setText("倒计时："+millisUntilFinished / 1000+" 秒");
            }

            @Override
            public void onFinish() {
                watchdog_count.setText("");
            }
        };
        countDownTimer.start();
    }

    private void isCountDownTimer() {
        if (countDownTimer != null){
            countDownTimer.cancel();
        }
    }

    public native int  startWatchdog();
    public native int  setWatchDogTimeout(String timeout);
    public native int  pingWatchdog();
    public native int  stopWatchdog();

    public native int getWatchDogTimeOut();
}
